package cn.itcast.chapter04.response;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ChineseServlet
 */
public class ChineseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
	    // 1.处理字节流的中文乱码问题
	    String data = "中国有句古话叫做，识时务者为俊杰";
	    ServletOutputStream out = response.getOutputStream();
	    // 定义一个字节流，然后使用他的write方法
	    out.write(data.getBytes());
	    // 2、处理乱码：服务器端的编码和浏览器端的编码保持一致
	    response.setHeader("Content-type", "text/html; charset = utf-8");
	    // 也可以简写
	    response.setContentType("text/html; charset = utf-8");
	    // 3、让服务器端的中文编码格式是utf-8
	    out.write(data.getBytes("utf-8"));
	    */
	    
	    // 处理字符流的中文乱码
	    String data1 = "中国万岁China!!!";
	    // getWriter()在返回PrintWriter对象时就已经将CharacterEncoding确定为默认的编码方式"ISO-8859-1"了
	    // 所以需要把下面这句放到设置编码之后
	    PrintWriter out1 = response.getWriter();
	    // 第一种方法
	    // 1、处理服务器的编码，让这个编码支持中文
	    response.setCharacterEncoding("utf-8");
	    // 2、处理浏览器端的编码
	    response.setHeader("Content-type", "text/html; charset = utf-8");
	    // 第二种方法
	    response.setContentType("text/html; charset = utf-8");
	    
	    out1.println(data1);
	    
	    
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
